// 引入接口文件
import { getCommunityList, getCommunity, delCommunity, saveCommunityOrUpdate} from './oldCommunityServ.js'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import { isEmpty, isArray, cloneDeep, merge } from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'
import { makeAddress } from "utils/util"
import { download } from '../../../../../utils/util.js';
//打包文件到压缩包
// import JSZip from 'jszip';
// import saveAs from 'file-saver';
// import $ from 'jquery';

// import toZip from 'utils/jszip'

// import toZip from '../../../../../utils/jszip'


// 初始默认状态
const defultState = {
        loading: false,
        btnLoading: false,
        biz: {
            queryForm: {}
        },
        community: [],
        currentImg: '',
        currentItem: {},
        productId: '',
        qrcodeCount: '',
        codeId: '',
        channelId: '',
        channelType: [],
        zysqList: [],
        showAdv: null,
        zysqUrl: `subPackages\/pages\/buildingDetail\/buildingDetail`
            // zysqUrl: `pages\/load\/load`,
            // channelType:[
            //   {
            //     "id": 101,
            //     "name": "线上美的置业云首页"
            // },
            // {
            //     "id": 102,
            //     "name": "线上项目首页"
            // },
            // {
            //     "id": 103,
            //     "name": "线上报纸广告"
            // },
            // {
            //     "id": 104,
            //     "name": "线上软文广告"
            // },
            // {
            //     "id": 105,
            //     "name": "线上杂志广告"
            // },
            // {
            //     "id": 106,
            //     "name": "线上电视广告"
            // },
            // {
            //     "id": 107,
            //     "name": "线上电台广告"
            // },
            // {
            //     "id": 108,
            //     "name": "线上网络广告"
            // },
            // {
            //     "id": 109,
            //     "name": "线上新媒体广告"
            // },
            // {
            //     "id": 110,
            //     "name": "线上高空户外广告"
            // },
            // {
            //     "id": 111,
            //     "name": "线上低空户外广告"
            // },
            // {
            //     "id": 112,
            //     "name": "线上室内户内广告"
            // },
            // {
            //     "id": 113,
            //     "name": "线上交通类广告"
            // },
            // {
            //     "id": 114,
            //     "name": "线下展点单张"
            // },
            // {
            //     "id": 115,
            //     "name": "线下拓展派单"
            // },
            // {
            //     "id": 116,
            //     "name": "线下案场包装"
            // },
            // {
            //     "id": 117,
            //     "name": "线下看楼车"
            // }
            // ]
    }
    // 声明module
const tmpModule = {
    namespace: 'baseinfoModel',
    state: cloneDeep(defultState),
    subscriptions: {
        setup({ dispatch, history }) {
            history.listen(({ pathname, query }) => {
                if ('' + tmpModule.pathname === '' + pathname) {
                    dispatch({ type: 'updateStore', payload: cloneDeep(defultState) });
                }
            })
        },
    },
    // 方法列表
    effects: {
        * getCommunity({ payload }, { put, call, select }) {
            const { data, success } = yield call(getCommunity, payload);
            if (success) {
                let community = data
                yield put({ type: 'updateStore', payload: { community } })
            }
        },
        * deleteCommunity({ payload }, { put, call, select }) {
            try {
                const { data, success } = yield call(delCommunity, payload);
                let defultState = yield select(state => state[tmpModule.namespace])
                if (success) {
                    message.success('删除成功', () => {
                        defultState.biz.refreshListDatas(true)
                    })
                } else {

                }
            } catch (e) {

            }
        },
        
        
        //新增推荐楼盘
        *create({ payload }, { put, call, select }) {
            try{
            let {quer} = payload
            let obj = {
                resourceId: quer.resourceId,
                validityEndTime: quer.validityEndTime,
                validityStartTime: quer.validityStartTime,
                productTag: quer.productTag
            }
            const { success } = yield call(homeRecommendCreate, obj)
                if(success){
                    message.success('操作成功', () => {
                        defultState.biz.refreshListDatas(true)
                    })
                }
            }catch(e){
                console.error(e)
            }
        },
        * qrcodeCreate({ payload }, { put, call, select }) {
            try {
                let { codeId, channelId, currentItem } = yield select(state => state[tmpModule.namespace])
                const { data, success } = yield call(qrcodeCreate, { productId: currentItem.productId, ...payload });
                if (success) {
                    console.log(data)
                    yield put({
                        type: 'shareImg',
                        payload: { productId: currentItem.productId, channelType: payload.channelType }
                    })
                }
            } catch (e) {

            }
        }
    },



    // 存入状态机
    reducers: {
        updateStore(preState, action) {
            return {...preState, ...action.payload };
        }
    }
}

export default tmpModule